<template>
  <a-modal
    :title="title"
    :width="1380"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-row>
        <a-col span="9">
          <a-form-model ref="form" v-bind="layout" :model="model" :rules="validatorRules">
            <a-form-model-item label="分类" required prop="projectType">
              <a-radio-group v-model="model.projectType">
                <a-radio value="1">
                  治疗
                </a-radio>
                <a-radio value="2">
                  理疗
                </a-radio>
              </a-radio-group>
              <div class="ant-form-explain">治疗项目，通常为注射、输液、露化等一殷门诊项目</div>
            </a-form-model-item>
            <a-form-model-item label="项目名称" required prop="projectName">
              <a-input placeholder="请输入项目名称" v-model="model.projectName" />
            </a-form-model-item>
            <a-form-model-item label="项目编码">
              <a-input placeholder="请输入项目编码或系统自动生成" v-model="model.projectCode" />
            </a-form-model-item>
            <a-form-model-item label="执行划扣" required prop="izMinus">
              <a-select v-model="model.izMinus" placeholder="请选择是否执行划扣">
                <a-select-option value="">请选择</a-select-option>
                <a-select-option value="0">不需要</a-select-option>
                <a-select-option value="1">需要</a-select-option>
              </a-select>
            </a-form-model-item>
            <a-form-model-item label="单位 " required prop="unit">
              <a-input placeholder="请输入单位 " v-model="model.unit" />
            </a-form-model-item>
            <a-form-model-item label="成本价" required prop="costAmount">
              <a-input-number placeholder="请输入成本价" v-model="model.costAmount" style="width: 150px" />
            </a-form-model-item>
            <a-form-model-item label="销售价" required prop="saleAmount">
              <a-input-number placeholder="请输入销售价" v-model="model.saleAmount" style="width: 150px" />
            </a-form-model-item>
          </a-form-model>
        </a-col>
        <a-col  span="15">
          <h3>医保对码</h3>
          <a-table
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            :columns="columns">
          </a-table>
        </a-col>
      </a-row>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction, putAction, getAction, postAction } from '@/api/manage'

export default {
  name: 'hHisProjectModal',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {
        categoryCode: '1',
        unit: '次',
        projectType: '1'
      },
      layout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 14 }
      },
      confirmLoading: false,
      validatorRules: {
        projectType: [{ required: true, message: '请输入分类!' }],
        projectName: [{ required: true, message: '请输入项目名称!' }],
        projectCode: [{ required: true, message: '请输入项目编码!' }],
        izMinus: [{ required: true, message: '请输入执行划扣' }],
        unit: [{ required: true, message: '请输入单位!' }],
        costAmount: [{ required: true, message: '请输入成本价!' }],
        saleAmount: [{ required: true, message: '请输入销售价!' }]
      },
      columns: [
        {
          title: '对码项目',
          align: 'center',
          dataIndex: 'projectName',
          width: 130,
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'projectType',
          customRender: function(t, r, index) {
            return t == '1' ? '治疗' : '理疗'
          },
          width: 100,
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'costAmount',
          customRender: function(t, r, index) {
            return t == 0 ? '~' :'￥'+t;
          },
          width: 100,
        },
        {
          title: '医保限价',
          align: 'center',
          dataIndex: 'saleAmount',
          customRender: function(t, r, index) {
            return t == 0 ? '~' :'￥'+t;
          },
          width: 100,
        },
        {
          title: '单价',
          align: 'center',
          dataIndex: 'unit',
          width: 100,
        },
        {
          title: '售价',
          align: 'center',
          dataIndex: 'projectCode',
          width: 100,
        },
        {
          title: '医保支付',
          align: 'center',
          dataIndex: 'izMinus',
          customRender: function(t, r, index) {
            return t == '1' ? '需要' : '不需要'
          },
          width: 100,
        },

      ],
      url: {
        add: '/his/project',
        edit: '/his/project'
      }
    }
  },
  created() {
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$refs.form.clearValidate()
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
              that.close()
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    }

  }
}
</script>

<style scoped>

</style>